package com.pkdl.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.pkdl.dto.PhieuKham_ThuocDTO;

// Data access object, which get data of PhieuKham_Thuoc table from database
public class PhieuKham_ThuocDAO {
	public boolean delete(int mapkthuoc) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_PhieuKham_Thuoc_Delete(?)}");
			cs.setInt(1, mapkthuoc);
			int n = cs.executeUpdate();
			if (n == 0) {
				result = false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(PhieuKham_ThuocDAO.class.getName()).log(
					Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(PhieuKham_ThuocDAO.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}
		return result;
	}

	public boolean deleteByMaPhieuKham(int mapk) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_PhieuKham_Thuoc_DeleteByMaPhieuKham(?)}");
			cs.setInt(1, mapk);
			int n = cs.executeUpdate();
			if (n == 0) {
				result = false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(PhieuKham_ThuocDAO.class.getName()).log(
					Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(PhieuKham_ThuocDAO.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}
		return result;
	}

	public boolean insert(PhieuKham_ThuocDTO phieukham_thuoc) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_PhieuKham_Thuoc_Insert(?, ?, ?)}");
			cs.setInt(1, phieukham_thuoc.getMaPK());
			cs.setInt(2, phieukham_thuoc.getMaThuoc());
			cs.setInt(3, phieukham_thuoc.getSoLuong());
			int n = cs.executeUpdate();
			if (n == 0) {
				result = false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(PhieuKham_ThuocDAO.class.getName()).log(
					Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(PhieuKham_ThuocDAO.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}
		return result;
	}

	public ArrayList<PhieuKham_ThuocDTO> selectAll() {
		ArrayList<PhieuKham_ThuocDTO> list = new ArrayList<PhieuKham_ThuocDTO>();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_PhieuKham_Thuoc_SelectAll()}");
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				PhieuKham_ThuocDTO phieukham_thuoc = new PhieuKham_ThuocDTO();
				phieukham_thuoc.setMaPKThuoc(rs.getInt("MaPKThuoc"));
				phieukham_thuoc.setMaPK(rs.getInt("MaPK"));
				phieukham_thuoc.setMaThuoc(rs.getInt("MaThuoc"));
				phieukham_thuoc.setSoLuong(rs.getInt("SoLuong"));
				list.add(phieukham_thuoc);
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(PhieuKham_ThuocDAO.class.getName()).log(
					Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(PhieuKham_ThuocDAO.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}
		return list;
	}

	public PhieuKham_ThuocDTO selectById(int mapkthuoc) {
		PhieuKham_ThuocDTO phieukham_thuoc = new PhieuKham_ThuocDTO();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_PhieuKham_Thuoc_SelectByID(?)}");
			cs.setInt(1, mapkthuoc);
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				phieukham_thuoc.setMaPKThuoc(rs.getInt("MaPKThuoc"));
				phieukham_thuoc.setMaPK(rs.getInt("MaPK"));
				phieukham_thuoc.setMaThuoc(rs.getInt("MaThuoc"));
				phieukham_thuoc.setSoLuong(rs.getInt("SoLuong"));
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(PhieuKham_ThuocDAO.class.getName()).log(
					Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(PhieuKham_ThuocDAO.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}
		return phieukham_thuoc;
	}

	public ArrayList<PhieuKham_ThuocDTO> selectByMaPK(int mapk) {
		ArrayList<PhieuKham_ThuocDTO> list = new ArrayList<PhieuKham_ThuocDTO>();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_PhieuKham_Thuoc_SelectByMaPK(?)}");
			cs.setInt(1, mapk);
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				PhieuKham_ThuocDTO phieukham_thuoc = new PhieuKham_ThuocDTO();
				phieukham_thuoc.setMaPKThuoc(rs.getInt("MaPKThuoc"));
				phieukham_thuoc.setMaPK(mapk);
				phieukham_thuoc.setMaThuoc(rs.getInt("MaThuoc"));
				phieukham_thuoc.setTenThuoc(rs.getString("TenThuoc"));
				phieukham_thuoc.setSoLuong(rs.getInt("SoLuong"));
				list.add(phieukham_thuoc);
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(PhieuKham_ThuocDAO.class.getName()).log(
					Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(PhieuKham_ThuocDAO.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}
		return list;
	}

	public ArrayList<PhieuKham_ThuocDTO> selectByMaThuoc(int mathuoc) {
		ArrayList<PhieuKham_ThuocDTO> list = new ArrayList<PhieuKham_ThuocDTO>();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_PhieuKham_Thuoc_SelectByMaThuoc(?)}");
			cs.setInt(1, mathuoc);
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				PhieuKham_ThuocDTO phieukham_thuoc = new PhieuKham_ThuocDTO();
				phieukham_thuoc.setMaPKThuoc(rs.getInt("MaPKThuoc"));
				phieukham_thuoc.setMaPK(rs.getInt("MaPK"));
				phieukham_thuoc.setMaThuoc(rs.getInt("MaThuoc"));
				phieukham_thuoc.setSoLuong(rs.getInt("SoLuong"));
				list.add(phieukham_thuoc);
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(PhieuKham_ThuocDAO.class.getName()).log(
					Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(PhieuKham_ThuocDAO.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}
		return list;
	}

	public boolean update(PhieuKham_ThuocDTO phieukham_thuoc) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_PhieuKham_Thuoc_Update(?, ?, ?, ?)}");
			cs.setInt(1, phieukham_thuoc.getMaPKThuoc());
			cs.setInt(2, phieukham_thuoc.getMaPK());
			cs.setInt(3, phieukham_thuoc.getMaThuoc());
			cs.setInt(4, phieukham_thuoc.getSoLuong());
			int n = cs.executeUpdate();
			if (n == 0) {
				result = false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(PhieuKham_ThuocDAO.class.getName()).log(
					Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(PhieuKham_ThuocDAO.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}
		return result;
	}

}
